package com.yiwei.jmx.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.model.DealListV3Model;


import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyBorrowRecordAdapter extends ItemAdapter<DealListV3Model.DataBean, MyBorrowRecordAdapter.DefaultHolder> {


    public MyBorrowRecordAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_borrow_record, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        DealListV3Model.DataBean item = getItem(position);
//        "status": 4      // 0 - 申请中，1 - 招标中，2 - 待放款，3 - 审核失败，4 - 还款中，5 - 已还清
        switch (item.status) {
            case "0":
                holder.txtStatus.setText("审核中");
                holder.txtStatus.setShapeSolidColor(0xffFF7A31).setUseShape();
                holder.btnWechat.setText("查看进度");
                holder.btnWechat.setShapeSolidColor(0xff0072FF).setUseShape();
                break;
            case "1":
                holder.txtStatus.setText("招标中");
                holder.txtStatus.setShapeSolidColor(0xffFF7A31).setUseShape();
                holder.btnWechat.setText("查看进度");
                holder.btnWechat.setShapeSolidColor(0xff0072FF).setUseShape();
                break;
            case "2":
                holder.txtStatus.setText("待放款");
                holder.txtStatus.setShapeSolidColor(0xffFFC231).setUseShape();
                holder.btnWechat.setText("查看进度");
                holder.btnWechat.setShapeSolidColor(0xff0072FF).setUseShape();
                break;
            case "3":
                holder.txtStatus.setText("审核失败");
                holder.txtStatus.setShapeSolidColor(0xffFE5A59).setUseShape();
                holder.txtOrderNo.setText("订单编号：" + item.deal_sn + "\n失败原因：" + item.delete_msg);
                holder.btnWechat.setText("查看原因");
                holder.btnWechat.setShapeSolidColor(0xff0072FF).setUseShape();
                break;
            case "4":
                holder.txtStatus.setText("还款中");
                holder.txtStatus.setShapeSolidColor(0xffFF7A31).setUseShape();
                if (Float.valueOf(item.impose_day) > 0) {
                    holder.btnWechat.setText("立即还款");
                    holder.btnWechat.setShapeSolidColor(0xffFF2740).setUseShape();
                } else {
                    holder.btnWechat.setText("提前还款");
                    holder.btnWechat.setShapeSolidColor(0xff0AA745).setUseShape();
                }

                break;
            case "5":
                holder.txtStatus.setText("还款成功");
                holder.txtStatus.setShapeSolidColor(0xff3BCF8F).setUseShape();
                holder.btnWechat.setText("再次借款");
                holder.btnWechat.setShapeSolidColor(0xffFF7E00).setUseShape();
                break;
        }


        if (item.status.equals("4")) {
            holder.txtInfoLeft.setText("借款金额:￥" + item.borrow_amount + "\n应还日期:" + item.repay_date);
            holder.txtInfoRight.setText("借款期限:" + item.repay_time + "天\n逾期费用:" + item.impose_fee);
        } else if (item.status.equals("5")) {
            if (item.renewal_num.equals("0")) {
                holder.txtInfoLeft.setText("借款金额:￥" + item.borrow_amount + "\n应还日期:" + item.repay_date);
                holder.txtInfoRight.setText("借款期限:" + item.repay_time + "\n逾期费用:" + item.impose_fee);
            } else {
                holder.txtInfoLeft.setText("借款金额:￥" + item.borrow_amount + "\n续期费用:" + item.renewal_total_money + "元\n应还日期:" + item.repay_date);
                holder.txtInfoRight.setText("借款期限:" + item.repay_time + "天\n续期次数：" + item.renewal_num + "\n逾期费用:" + item.impose_fee);
            }
        } else {
            holder.txtInfoLeft.setText("借款金额:￥" + item.borrow_amount + "\n应还日期:" + item.repay_date);
            holder.txtInfoRight.setText("借款期限:" + item.repay_time + "天");

        }

        holder.txtBorrowTime.setText(item.create_date);
        holder.btnWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClickCheck(item.status, item.id);
                }
            }
        });
        holder.btnHetong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClickHetong(item.id);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.txt_status)
        SuperButton txtStatus;
        @BindView(R.id.txt_info_left)
        TextView txtInfoLeft;
        @BindView(R.id.txt_info_right)
        TextView txtInfoRight;
        @BindView(R.id.layout_info)
        FrameLayout layoutInfo;
        @BindView(R.id.txt_order_no)
        TextView txtOrderNo;
        @BindView(R.id.btn_hetong)
        TextView btnHetong;
        @BindView(R.id.txt_borrow_time)
        TextView txtBorrowTime;
        @BindView(R.id.btn_check)
        SuperButton btnWechat;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClickHetong(String id);

        void onClickCheck(String status, String id);
    }
}
